package com.leyou.item.controller;

import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import com.leyou.pojo.PageResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 岩冰
 * @version 1.0
 * @date:2018/9/25
 */
@RestController
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @GetMapping("page")
    public ResponseEntity<PageResult> queryPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "sortBy",required =false) String sortBy,
            @RequestParam(value = "desc",defaultValue = "false")Boolean desc,
            @RequestParam(value = "key",required =false)String key) {
        PageResult<Brand> pageResult = brandService.queryPage(page, rows, sortBy, desc, key);
        if (pageResult==null|| pageResult.getItems().size()==0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(pageResult);
    }
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandsByCid(@PathVariable("cid")Long cid) {
        List<Brand> brandList = brandService.queryBrandsByCid(cid);
        if (brandList==null|| brandList.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(brandList);
    }

    @PostMapping
    public ResponseEntity<Void> addBrand(Brand brand,@RequestParam("cids")Long[]cids) {
        brandService.addBrand(brand,cids);
        return ResponseEntity.ok().build();
    }
    @PutMapping
    public ResponseEntity<Void> editBrand(Brand brand,@RequestParam("cids") List<Long>cids) {
        brandService.editBrand(brand,cids);
        return ResponseEntity.ok().build();
    }
    @DeleteMapping("{bid}")
    public ResponseEntity<Void> editBrand(@PathVariable("bid")Long bid) {
        brandService.deleteBrandById(bid);
        return ResponseEntity.ok().build();
    }

    @GetMapping("list")
   private ResponseEntity< List<Brand>> queryBrandByIds(@RequestParam("ids") List<Long> ids){
        List<Brand> brands= brandService.queryBrandByIds(ids);
        if (brands==null||brands.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(brands);
    }
}
